//#define ENABLE_BIT_DEFINITIONS
#include <ioavr.h>
#include <stdbool.h>
#include <intrinsics.h>

//PB3 Output TEMPO  pin2
//PB4 Output FAN    pin3
//PB0 Input  COMP   pin5

#define COMP 0
#define TEMPO 3
#define FAN 4
#define TIME_ON 8771 //One minute
int timer;
volatile bool gen_on;
volatile bool comp_on;

void init(void);

#pragma vector=0x03 * 2
__interrupt void timer_overflow(void)    //  6.840 ms 8Mhz
{
  timer++;
   if(gen_on)
   {
     //Reverse PORT TEMPO
   if(PORTB&(1<<TEMPO)) PORTB&=~(1<<TEMPO);
    else PORTB|=(1<<TEMPO);
   }

}

void main( void )
{

      init();
      gen_on=0;
      PORTB|=(1<<TEMPO); //TEMPO-Off
        PORTB|=(1<<FAN); //FAN ON
    // Main Loop
     while(1)
      {
         if(PINB&(1<<COMP))  //      COMP<<1
         {
           gen_on=0;
           timer=0;
           PORTB|=(1<<FAN); //FAN ON
           PORTB|=(1<<TEMPO); //TEMPO-Off
         }
          else
          {
            if(timer > TIME_ON)
            {
              timer=TIME_ON+10;
              gen_on=1;
               PORTB&=~(1<<FAN); //FAN OFF
            }

          }
      }
}


void init()
{
     //Init DDRB
    MCUCR&=~(1<<PUD);
    PORTB|=(1<<COMP);
   DDRB=(1<<FAN|1<<TEMPO);  //Output
    //Init Timer0 clk/256 4.8/256 =0.208us CS02=1;CS01=0;CS00=0
   TCCR0A = 0x00; // normal operation (mode0);
   TCNT0=0x0000;
   TIMSK0=(1<<TOIE0);
   TCCR0B=(1<<CS02);   //Start timer
   __enable_interrupt();

}



